﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace SizeboxUI
{
	// Token: 0x020003C1 RID: 961
	public class SceneTreeView : MonoBehaviour
	{
		// Token: 0x170003B7 RID: 951
		// (get) Token: 0x06001A32 RID: 6706 RVA: 0x000A3B78 File Offset: 0x000A1D78
		// (set) Token: 0x06001A33 RID: 6707 RVA: 0x000A3B80 File Offset: 0x000A1D80
		public Sprite icon_giantess { get; private set; }

		// Token: 0x170003B8 RID: 952
		// (get) Token: 0x06001A34 RID: 6708 RVA: 0x000A3B89 File Offset: 0x000A1D89
		// (set) Token: 0x06001A35 RID: 6709 RVA: 0x000A3B91 File Offset: 0x000A1D91
		public Sprite icon_micro { get; private set; }

		// Token: 0x170003B9 RID: 953
		// (get) Token: 0x06001A36 RID: 6710 RVA: 0x000A3B9A File Offset: 0x000A1D9A
		// (set) Token: 0x06001A37 RID: 6711 RVA: 0x000A3BA2 File Offset: 0x000A1DA2
		public Sprite icon_object { get; private set; }

		// Token: 0x170003BA RID: 954
		// (get) Token: 0x06001A38 RID: 6712 RVA: 0x000A3BAB File Offset: 0x000A1DAB
		// (set) Token: 0x06001A39 RID: 6713 RVA: 0x000A3BB3 File Offset: 0x000A1DB3
		public Sprite icon_category { get; private set; }

		// Token: 0x170003BB RID: 955
		// (get) Token: 0x06001A3A RID: 6714 RVA: 0x000A3BBC File Offset: 0x000A1DBC
		// (set) Token: 0x06001A3B RID: 6715 RVA: 0x000A3BC4 File Offset: 0x000A1DC4
		public Sprite icon_player { get; private set; }

		// Token: 0x06001A3C RID: 6716 RVA: 0x000A3BD0 File Offset: 0x000A1DD0
		private void Awake()
		{
			this.lastSceneChecksum = 0m;
			this.gridGroup = base.GetComponentInChildren<GridLayoutGroup>();
			this.treeEntry = Resources.Load<GameObject>("UI/Edit/SceneTree/SceneTreeElement");
			this.icon_giantess = Resources.Load<Sprite>("UI/Edit/SceneTree/giantess");
			this.icon_micro = Resources.Load<Sprite>("UI/Edit/SceneTree/micro");
			this.icon_object = Resources.Load<Sprite>("UI/Edit/SceneTree/object");
			this.icon_category = Resources.Load<Sprite>("UI/Edit/SceneTree/category");
			this.icon_player = Resources.Load<Sprite>("UI/Edit/SceneTree/player");
		}

		// Token: 0x06001A3D RID: 6717 RVA: 0x000A3C55 File Offset: 0x000A1E55
		private void OnEnable()
		{
			this.Refresh();
		}

		// Token: 0x06001A3E RID: 6718 RVA: 0x000A3C55 File Offset: 0x000A1E55
		private void OnDisable()
		{
			this.Refresh();
		}

		// Token: 0x06001A3F RID: 6719 RVA: 0x000A3C60 File Offset: 0x000A1E60
		private void Refresh()
		{
			if (this.gridGroup == null)
			{
				return;
			}
			if (base.gameObject.activeSelf)
			{
				this.PopulateTree();
				GameObject[] rootGameObjects = SceneManager.GetActiveScene().GetRootGameObjects();
				this.oldObjectCount = rootGameObjects.Length;
				base.InvokeRepeating("ListUpdate", 0f, 0.5f);
			}
			else
			{
				base.CancelInvoke("ListUpdate");
				this.oldObjectCount = 0;
				for (int i = 0; i < this.categories.Length; i++)
				{
					if (this.categories[i] != null)
					{
						this.categories[i].GetComponent<ST_EntryManager>().SetCategoryExpanded(true, true);
					}
				}
				ST_EntryManager[] componentsInChildren = this.gridGroup.GetComponentsInChildren<ST_EntryManager>();
				for (int j = 0; j < componentsInChildren.Length; j++)
				{
					UnityEngine.Object.Destroy(componentsInChildren[j].gameObject);
				}
				for (int k = 0; k < this.categories.Length; k++)
				{
					this.categories[k] = null;
				}
			}
		}

		// Token: 0x06001A40 RID: 6720 RVA: 0x000A3D74 File Offset: 0x000A1F74
		public void SetSelectedByObject(GameObject obj, bool val)
		{
			for (int i = 0; i < this.categories.Length; i++)
			{
				if (!(this.categories[i] == null))
				{
					ST_EntryManager entryByObject = this.categories[i].GetComponent<ST_EntryManager>().GetEntryByObject(obj);
					if (entryByObject != null)
					{
						if (this.IsEntrySelected(entryByObject.gameObject) != val)
						{
							this.ToggleSelected(entryByObject.gameObject, false);
						}
						return;
					}
				}
			}
		}

		// Token: 0x06001A41 RID: 6721 RVA: 0x000A3DF2 File Offset: 0x000A1FF2
		public void SetSelected(GameObject entry, bool val)
		{
			if (!entry.transform.IsChildOf(this.gridGroup.transform))
			{
				return;
			}
			if (this.IsEntrySelected(entry) != val)
			{
				this.ToggleSelected(entry, false);
			}
		}

		// Token: 0x06001A42 RID: 6722 RVA: 0x000A3E28 File Offset: 0x000A2028
		public void ToggleSelectedByObject(GameObject obj, bool clearOtherSelected = true)
		{
			for (int i = 0; i < this.categories.Length; i++)
			{
				if (!(this.categories[i] == null))
				{
					ST_EntryManager entryByObject = this.categories[i].GetComponent<ST_EntryManager>().GetEntryByObject(obj);
					if (entryByObject != null)
					{
						this.ToggleSelected(entryByObject.gameObject, clearOtherSelected);
						return;
					}
				}
			}
		}

		// Token: 0x06001A43 RID: 6723 RVA: 0x000A3E94 File Offset: 0x000A2094
		public void ToggleSelected(GameObject entry, bool clearOtherSelected = true)
		{
			if (!entry.transform.IsChildOf(this.gridGroup.transform))
			{
				return;
			}
			if (clearOtherSelected)
			{
				for (int i = 0; i < this.gridGroup.transform.childCount; i++)
				{
					if (!(this.gridGroup.transform.GetChild(i).gameObject == entry) && !this.gridGroup.transform.GetChild(i).GetComponent<ST_EntryManager>().category)
					{
						this.gridGroup.transform.GetChild(i).GetComponent<Image>().color = new Color(1f, 1f, 1f, 0.39f);
					}
				}
			}
			entry.GetComponent<Image>().color = ((!this.IsEntrySelected(entry)) ? new Color(0f, 0.88f, 1f, 0.88f) : new Color(1f, 1f, 1f, 0.39f));
		}

		// Token: 0x06001A44 RID: 6724 RVA: 0x000A3FAC File Offset: 0x000A21AC
		public bool IsEntrySelected(GameObject entry)
		{
			return entry.transform.IsChildOf(this.gridGroup.transform) && entry.GetComponent<Image>().color == new Color(0f, 0.88f, 1f, 0.88f);
		}

		// Token: 0x06001A45 RID: 6725 RVA: 0x000A4000 File Offset: 0x000A2200
		public int GetSortScore(ST_EntryManager.EntityDisplayType type)
		{
			for (int i = 0; i < SceneTreeView.sortOrder.Length; i++)
			{
				if (SceneTreeView.sortOrder[i] == type)
				{
					return i;
				}
			}
			return -1;
		}

		// Token: 0x06001A46 RID: 6726 RVA: 0x000A4038 File Offset: 0x000A2238
		public string GetSortName(ST_EntryManager.EntityDisplayType type)
		{
			for (int i = 0; i < SceneTreeView.sortOrder.Length; i++)
			{
				if (SceneTreeView.sortOrder[i] == type)
				{
					return SceneTreeView.sortNames[i];
				}
			}
			return "<Unknown>";
		}

		// Token: 0x06001A47 RID: 6727 RVA: 0x000A4077 File Offset: 0x000A2277
		private ST_EntryManager GetCategoryEntryManager(ST_EntryManager.EntityDisplayType type)
		{
			return (!(this.categories[this.GetSortScore(type)] == null)) ? this.categories[this.GetSortScore(type)].GetComponent<ST_EntryManager>() : null;
		}

		// Token: 0x06001A48 RID: 6728 RVA: 0x000A40AC File Offset: 0x000A22AC
		public void RemoveEntry(GameObject go)
		{
			IEnumerator enumerator = this.gridGroup.transform.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					object obj = enumerator.Current;
					Transform transform = (Transform)obj;
					ST_EntryManager component = transform.GetComponent<ST_EntryManager>();
					if (!(component.obj != go))
					{
						ST_EntryManager categoryEntryManager = this.GetCategoryEntryManager(component.type);
						for (int i = 0; i < categoryEntryManager.categoryChildren.Count; i++)
						{
							if (!(categoryEntryManager.categoryChildren[i] != go))
							{
								categoryEntryManager.categoryChildren.RemoveAt(i);
								categoryEntryManager.UpdateCategory();
								break;
							}
						}
						UnityEngine.Object.Destroy(transform.gameObject);
						break;
					}
				}
			}
			finally
			{
				IDisposable disposable;
				if ((disposable = (enumerator as IDisposable)) != null)
				{
					disposable.Dispose();
				}
			}
		}

		// Token: 0x06001A49 RID: 6729 RVA: 0x000A41A0 File Offset: 0x000A23A0
		public void AddEntry(GameObject go, bool categorySearch = true)
		{
			EntityBase component = go.GetComponent<EntityBase>();
			GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.treeEntry);
			gameObject.GetComponentInChildren<Text>().text = component.model;
			Sprite sprite = (!component.isPlayer) ? ((!component.isGiantess) ? ((!component.isMicro) ? this.icon_object : this.icon_micro) : this.icon_giantess) : this.icon_player;
			gameObject.GetComponentsInChildren<Image>()[1].sprite = sprite;
			ST_EntryManager st_EntryManager = gameObject.AddComponent<ST_EntryManager>();
			st_EntryManager.obj = go;
			st_EntryManager.stv = this;
			st_EntryManager.type = ((!component.isGiantess) ? ((!component.isMicro) ? ST_EntryManager.EntityDisplayType.Object : ST_EntryManager.EntityDisplayType.Micro) : ST_EntryManager.EntityDisplayType.Giantess);
			st_EntryManager.category = false;
			gameObject.transform.SetParent(this.gridGroup.transform, false);
			if (!categorySearch)
			{
				return;
			}
			for (int i = 0; i < this.gridGroup.transform.childCount; i++)
			{
				GameObject gameObject2 = this.gridGroup.transform.GetChild(i).gameObject;
				ST_EntryManager component2 = gameObject2.GetComponent<ST_EntryManager>();
				if (component2.category && component2.categoryType == st_EntryManager.type)
				{
					component2.categoryChildren.Add(gameObject);
					gameObject.transform.SetSiblingIndex(i + component2.categoryChildren.Count);
					if (!component2.categoryExpanded)
					{
						gameObject.SetActive(false);
					}
					component2.UpdateCategory();
					return;
				}
			}
			this.CreateCategories();
		}

		// Token: 0x06001A4A RID: 6730 RVA: 0x000A4340 File Offset: 0x000A2540
		private void CreateCategories()
		{
			for (int i = 0; i < SceneTreeView.sortOrder.Length; i++)
			{
				if (!(this.categories[i] != null))
				{
					int num = -1;
					ST_EntryManager[] componentsInChildren = this.gridGroup.GetComponentsInChildren<ST_EntryManager>();
					for (int j = 0; j < componentsInChildren.Length; j++)
					{
						GameObject gameObject = componentsInChildren[j].gameObject;
						ST_EntryManager component = gameObject.GetComponent<ST_EntryManager>();
						if (component == null || component.category || component.type != SceneTreeView.sortOrder[i])
						{
							if (num != -1)
							{
								break;
							}
						}
						else if (num == -1)
						{
							num = j;
						}
					}
					if (num != -1)
					{
						GameObject gameObject2 = UnityEngine.Object.Instantiate<GameObject>(this.treeEntry);
						gameObject2.GetComponentsInChildren<Image>()[1].sprite = this.icon_category;
						gameObject2.GetComponent<Image>().color = new Color(0.9f, 0f, 0f, 0.39f);
						ST_EntryManager st_EntryManager = gameObject2.AddComponent<ST_EntryManager>();
						st_EntryManager.obj = null;
						st_EntryManager.stv = this;
						st_EntryManager.type = ST_EntryManager.EntityDisplayType.Giantess;
						st_EntryManager.category = true;
						st_EntryManager.categoryType = SceneTreeView.sortOrder[i];
						st_EntryManager.categoryExpanded = true;
						st_EntryManager.categoryChildren = new List<GameObject>();
						for (int k = num; k < this.gridGroup.transform.childCount; k++)
						{
							GameObject gameObject3 = this.gridGroup.transform.GetChild(k).gameObject;
							ST_EntryManager component2 = gameObject3.GetComponent<ST_EntryManager>();
							if (component2.category || component2.type != SceneTreeView.sortOrder[i])
							{
								break;
							}
							st_EntryManager.categoryChildren.Add(gameObject3);
						}
						gameObject2.transform.SetParent(this.gridGroup.transform, false);
						gameObject2.transform.SetSiblingIndex(num);
						this.categories[i] = gameObject2;
						st_EntryManager.UpdateCategory();
					}
				}
			}
			for (int l = 0; l < this.categories.Length; l++)
			{
				if (this.categories[l] != null && !this.sortShown[l])
				{
					this.categories[l].GetComponent<ST_EntryManager>().SetCategoryExpanded(false, false);
				}
			}
		}

		// Token: 0x06001A4B RID: 6731 RVA: 0x000A4598 File Offset: 0x000A2798
		private void PopulateTree()
		{
			List<GameObject> list = new List<GameObject>();
			GameObject[] rootGameObjects = SceneManager.GetActiveScene().GetRootGameObjects();
			foreach (GameObject gameObject in rootGameObjects)
			{
				if (gameObject.activeInHierarchy)
				{
					if (!(gameObject.GetComponent<EntityBase>() == null) && !(gameObject.GetComponent<CameraReference>() != null))
					{
						list.Add(gameObject);
					}
				}
			}
			for (int j = 1; j < list.Count; j++)
			{
				EntityBase component = list[j].GetComponent<EntityBase>();
				EntityBase component2 = list[j - 1].GetComponent<EntityBase>();
				ST_EntryManager.EntityDisplayType type = (!component.isGiantess) ? ((!component.isMicro) ? ST_EntryManager.EntityDisplayType.Object : ST_EntryManager.EntityDisplayType.Micro) : ST_EntryManager.EntityDisplayType.Giantess;
				ST_EntryManager.EntityDisplayType type2 = (!component2.isGiantess) ? ((!component2.isMicro) ? ST_EntryManager.EntityDisplayType.Object : ST_EntryManager.EntityDisplayType.Micro) : ST_EntryManager.EntityDisplayType.Giantess;
				if (this.GetSortScore(type) < this.GetSortScore(type2))
				{
					list.RemoveAt(j);
					list.Insert(j - 1, component.gameObject);
					j = 0;
				}
			}
			list.ForEach(delegate(GameObject obj)
			{
				this.AddEntry(obj, false);
			});
			this.CreateCategories();
		}

		// Token: 0x06001A4C RID: 6732 RVA: 0x000A46F0 File Offset: 0x000A28F0
		public void ClearSelected()
		{
			List<ST_EntryManager> selectedEntries = this.GetSelectedEntries();
			selectedEntries.ForEach(delegate(ST_EntryManager mgr)
			{
				this.ToggleSelected(mgr.gameObject, false);
			});
		}

		// Token: 0x06001A4D RID: 6733 RVA: 0x000A4718 File Offset: 0x000A2918
		public List<ST_EntryManager> GetSelectedEntries()
		{
			List<ST_EntryManager> list = new List<ST_EntryManager>();
			for (int i = 0; i < this.gridGroup.transform.childCount; i++)
			{
				GameObject gameObject = this.gridGroup.transform.GetChild(i).gameObject;
				ST_EntryManager component = gameObject.GetComponent<ST_EntryManager>();
				if (this.IsEntrySelected(gameObject))
				{
					list.Add(component);
				}
			}
			return list;
		}

		// Token: 0x06001A4E RID: 6734 RVA: 0x000A4784 File Offset: 0x000A2984
		public void DeleteSelectedEntries()
		{
			List<ST_EntryManager> selectedEntries = this.GetSelectedEntries();
			selectedEntries.ForEach(delegate(ST_EntryManager mgr)
			{
				if (!mgr.obj.GetComponent<EntityBase>().isPlayer)
				{
					mgr.DeleteObject();
				}
			});
		}

		// Token: 0x06001A4F RID: 6735 RVA: 0x000A47BC File Offset: 0x000A29BC
		private decimal GetSceneChecksum(GameObject[] sceneObjects)
		{
			decimal num = 0m;
			for (int i = 0; i < sceneObjects.Length; i++)
			{
				GameObject gameObject = sceneObjects[i];
				if (!(gameObject.GetComponent<EntityBase>() == null) && !(gameObject.GetComponent<CameraReference>() != null))
				{
					EntityBase component = gameObject.GetComponent<EntityBase>();
					int num2 = 0;
					num2 |= ((!component.isPlayer) ? 0 : 1) << 0;
					num2 |= ((!component.isGiantess) ? 0 : 1) << 1;
					num2 |= ((!component.isMicro) ? 0 : 1) << 2;
					num2 += gameObject.name.GetHashCode();
					num += num2 * i;
				}
			}
			return num;
		}

		// Token: 0x06001A50 RID: 6736 RVA: 0x000A4888 File Offset: 0x000A2A88
		private void UpdateEntries()
		{
			for (int i = 0; i < this.categories.Length; i++)
			{
				if (!(this.categories[i] == null))
				{
					ST_EntryManager component = this.categories[i].GetComponent<ST_EntryManager>();
					component.categoryChildren.ForEach(delegate(GameObject obj)
					{
						obj.GetComponent<ST_EntryManager>().Refresh();
					});
				}
			}
		}

		// Token: 0x06001A51 RID: 6737 RVA: 0x000A48FC File Offset: 0x000A2AFC
		private void ListUpdate()
		{
			GameObject[] rootGameObjects = SceneManager.GetActiveScene().GetRootGameObjects();
			if (this.oldObjectCount == rootGameObjects.Length)
			{
				decimal sceneChecksum = this.GetSceneChecksum(rootGameObjects);
				if (this.lastSceneChecksum == sceneChecksum)
				{
					return;
				}
				this.lastSceneChecksum = sceneChecksum;
				this.UpdateEntries();
				return;
			}
			else
			{
				this.lastSceneChecksum = this.GetSceneChecksum(rootGameObjects);
				if (rootGameObjects.Length < this.oldObjectCount)
				{
					for (int i = 0; i < this.categories.Length; i++)
					{
						if (this.categories[i] != null)
						{
							this.categories[i].GetComponent<ST_EntryManager>().CleanupChildren();
						}
					}
					this.oldObjectCount = rootGameObjects.Length;
					this.UpdateEntries();
					return;
				}
				this.oldObjectCount = rootGameObjects.Length;
				foreach (GameObject gameObject in rootGameObjects)
				{
					if (!(gameObject.GetComponent<EntityBase>() == null) && !(gameObject.GetComponent<CameraReference>() != null))
					{
						for (int k = 0; k <= this.categories.Length; k++)
						{
							if (k == this.categories.Length)
							{
								this.AddEntry(gameObject, true);
								break;
							}
							if (!(this.categories[k] == null))
							{
								if (this.categories[k].GetComponent<ST_EntryManager>().HasObject(gameObject))
								{
									break;
								}
							}
						}
					}
				}
				this.UpdateEntries();
				return;
			}
		}

		// Token: 0x06001A52 RID: 6738 RVA: 0x000A4A80 File Offset: 0x000A2C80
		public void OnRightClick()
		{
			CommandView cView = base.GetComponentInParent<EditView>().actionSelector;
			InterfaceControl control = base.GetComponentInParent<EditView>().control;
			List<ST_EntryManager> sList = this.GetSelectedEntries();
			int count = sList.Count;
			cView.HideBehaviors();
			if (count == 1)
			{
				cView.AddCustomButton("Go to").onClick.AddListener(delegate()
				{
					PlayerCamera playerCamera = UnityEngine.Object.FindObjectOfType<PlayerCamera>();
					float num = sList[0].obj.transform.lossyScale.y * sList[0].obj.GetComponent<EntityBase>().meshHeight;
					playerCamera.transform.parent.SetPositionAndRotation(sList[0].obj.transform.position + new Vector3(0f, num / 2f, 0f) - playerCamera.transform.forward.normalized * (num * 1.4f), playerCamera.transform.rotation);
					cView.HideBehaviors();
				});
				cView.AddCustomButton("Select").onClick.AddListener(delegate()
				{
					control.SetSelectedObject(sList[0].obj.GetComponent<EntityBase>(), false);
					cView.HideBehaviors();
				});
				cView.AddCustomButton("Perform action...").onClick.AddListener(delegate()
				{
					cView.HideBehaviors();
					control.SetSelectedObject(sList[0].obj.GetComponent<EntityBase>(), false);
					cView.HackShowSelfBehaviors();
				});
			}
			if (count != 1 || !sList[0].obj.GetComponent<EntityBase>().isPlayer)
			{
				cView.AddCustomButton("Delete").onClick.AddListener(delegate()
				{
					this.DeleteSelectedEntries();
					cView.HideBehaviors();
				});
			}
			cView.AddCustomButton("Cancel").onClick.AddListener(delegate()
			{
				cView.HideBehaviors();
			});
			cView.OpenCommandMenu(true);
		}

		// Token: 0x04001C17 RID: 7191
		private GridLayoutGroup gridGroup;

		// Token: 0x04001C18 RID: 7192
		private GameObject treeEntry;

		// Token: 0x04001C1E RID: 7198
		public static ST_EntryManager.EntityDisplayType[] sortOrder = new ST_EntryManager.EntityDisplayType[]
		{
			ST_EntryManager.EntityDisplayType.Giantess,
			ST_EntryManager.EntityDisplayType.Micro,
			ST_EntryManager.EntityDisplayType.Object
		};

		// Token: 0x04001C1F RID: 7199
		public GameObject[] categories = new GameObject[3];

		// Token: 0x04001C20 RID: 7200
		private static string[] sortNames = new string[]
		{
			"Giantesses",
			"Micros",
			"Objects"
		};

		// Token: 0x04001C21 RID: 7201
		public bool[] sortShown = new bool[]
		{
			true,
			true,
			true
		};

		// Token: 0x04001C22 RID: 7202
		private decimal lastSceneChecksum;

		// Token: 0x04001C23 RID: 7203
		private int oldObjectCount;
	}
}
